package com.mc.metaSearch;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class rank {
	static 	GoogleSearch g = new GoogleSearch();
	static sougouSearch s = new sougouSearch();
	static BaiduSearch b = new BaiduSearch();
	static Search360 s3 = new Search360();
	static String[] gr;
	static Map<Integer, String> gurls;
	static Map<Integer, String> gabstracts;
	static Map<Integer, String> gtitles;
	static Map<Integer, String> gsubtitles;
	
	static String[] br;
	static Map<Integer, String> burls;
	static Map<Integer, String> babstracts;
	static Map<Integer, String> btitles;
	static Map<Integer, String> bsubtitles;
	
	static String[] sr;
	static Map<Integer, String> surls;
	static Map<Integer, String> sabstracts;
	static Map<Integer, String> stitles;
	
	public static List<String> titleResult;
	public static List<String> absResult;
	public static List<String> result;
//	static Map<String,float> gWeight;
	
	static public void culculateWeight(Map<Integer, String> gurls,Map<Integer, String> burls,Map<Integer, String> surls) throws IOException{
		double [][]arrayG=new double[gurls.size()][2];
		double [][]arrayB=new double[burls.size()][2];
	//	double [][]arrayS=new double[surls.size()][2];
		int size=gurls.size()+burls.size();//+surls.size();

		result=new LinkedList<String>();
		titleResult=new LinkedList<String>();
		absResult=new LinkedList<String>();
		double[][]array=new double[size][2];
		
		int i=0;
		int count=0;
		for(i=0;i<gurls.size();i++){
			arrayG[i][0]=(10-i*0.5)*0.4;
			arrayG[i][1]=i;
			array[count][0]=arrayG[i][0];
			array[count][1]=1;
			count++;
		}
		for(i=0;i<burls.size();i++){
			arrayB[i][0]=(10-i*0.5)*0.35;
			arrayB[i][1]=i;
			array[count][0]=arrayB[i][0];
			array[count][1]=2;
			count++;
		}
	/*	for(i=0;i<surls.size();i++){
			arrayS[i][0]=(10-i*0.5)*0.25;
			arrayS[i][1]=i;
			array[count][0]=arrayS[i][0];
			array[count][1]=3;
			count++;
		}*/
		double temp1=0,temp2=0;
		for(int j=0;j<size-1;j++){
			for(int k=0;k<size;k++){
				if(array[j][0]>=array[k][0]){
					temp1=array[j][0];
					temp2=array[j][1];
					array[j][0]=array[k][0];
					array[j][1]=array[k][1];
					array[k][0]=temp1;
					array[k][1]=temp2;		
				}
			}
		}
		for(i=0;i<size;i++){
			if(array[i][1]==1){
				for(int k=0;k<arrayG.length;k++){
					if(array[i][0]==arrayG[k][0])
					{
						if(!result.contains(gurls.get((int)arrayG[k][1]))){
							if(gurls.get((int)arrayG[k][1])!=null){
								if(gabstracts.get((int)arrayG[k][1])!=null){
									result.add(gurls.get((int)arrayG[k][1]));
									titleResult.add(gtitles.get((int)arrayG[k][1]));
									absResult.add(gabstracts.get((int)arrayG[k][1]));
								}
							}
							break;
						}
					}
				}
			}
			else if(array[i][1]==2){
				for(int k=0;k<arrayB.length;k++){
					if(array[i][0]==arrayB[k][0])
					{
						/*baiduURLparse bd = new baiduURLparse();
						String content=bd.getContent(burls.get((int)arrayB[k][1]));
						String url=bd.getURL(content);
						if(!result.contains(url)){
							if(burls.get((int)arrayB[k][1])!=null){
								result.add(url);
								titleResult.add(btitles.get((int)arrayB[k][1]));
								absResult.add(babstracts.get((int)array[k][1]));
							}
							break;
						}*/
					//	baiduURLparse bd = new baiduURLparse();
					//	String content=bd.getContent(burls.get((int)arrayB[k][1]));
					//	String url=bd.getURL(content);
						if(!result.contains(arrayB[k][0])){
							if(burls.get((int)arrayB[k][1])!=null){
								if(babstracts.get((int)array[k][1])!=null){
									result.add(burls.get((int)arrayB[k][1]));
									titleResult.add(btitles.get((int)arrayB[k][1]));
									absResult.add(babstracts.get((int)array[k][1]));
								}
							}
							break;
						}
					}
				}
			}
		/*	else if(array[i][1]==3){
				for(int k=0;k<arrayS.length;k++){
					if(array[i][0]==arrayS[k][0])
					{
						if(!result.contains(surls.get((int)arrayS[k][1]))){
							if(surls.get((int)arrayS[k][1])!=null){
								result.add(surls.get((int)arrayS[k][1]));
								titleResult.add(stitles.get((int)arrayS[k][1]));
								absResult.add(sabstracts.get((int)arrayS[k][1]));
							}	
							break;
						}
					}
				}
			}*/
		}
	}
   public void begin(){
		try { 
			/*double begin=System.currentTimeMillis();
			gr = g.getContent("西藏自然科学博物馆");
			gurls = g.getURL(gr);
			gabstracts = g.getAbstract(gr);
			gtitles = g.getTitle(gr);*/
		
			google_thread gt=new google_thread("西藏自然科学博物馆");
			gt.run();
			gurls = gt.gurls;
			gabstracts = gt.gabstracts;
			gtitles = gt.gtitles;
			Map<Integer, String> gsubtitles;
			gsubtitles=gt.gsubtitles;
	/*		
			baidu_thread bt=new baidu_thread("西藏自然科学博物馆");
			bt.run();
			burls = bt.burls;
			babstracts =bt.babstracts;
			btitles = bt.btitles;		
			Map<Integer, String> bsubtitles;
			bsubtitles=bt.bsubtitles;
			
			*/
					
			sr = s3.getContent("西藏自然科学博物馆");
			burls = s3.getURL(sr);
			babstracts = s3.getAbstract(sr);
			btitles =s3.getTitle(sr);
				
			culculateWeight(gurls,burls,surls);	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
